/********************************************************************************
 * RSSGeneratorAction.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Jan 9, 2009 3:01:05 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.Configuration;
import com.printeverywhere.dto.GeneralCategories;
import com.printeverywhere.dto.GeneralCategoriesWOS;
import com.printeverywhere.dto.GeneralOptions;
import com.printeverywhere.dto.ItemCategories;
import com.printeverywhere.dto.Items;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.RSSFeedGenerator;

public class RSSGeneratorAction extends ActionSupport implements ServletRequestAware {

	/**  */
	private static final long serialVersionUID = 1L;

	private String content;

	private HttpServletRequest request;

	public String execute() {
		String language = "en";
		int catId = 0;

		if(request.getParameter("lang") != null)
			language = request.getParameter("lang");

		if(request.getParameter("catId") != null) {
			try {
				catId = Integer.parseInt(request.getParameter("catId"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		List<Criterion> criterionList = new ArrayList<Criterion>();

		if(catId != 0) {
			List<Criterion> tmpCriterionList = new ArrayList<Criterion>();
			tmpCriterionList.add(Restrictions.eq("mainParentId", catId));
			criterionList.add(Restrictions.in("categoryId", CommonFunctionDelegator.getObjectsByCriterion(tmpCriterionList, null, null, GeneralCategoriesWOS.class)));
		}

		Configuration configuration = (Configuration) ActionContext.getContext().getApplication().get(AdminConstants.CONFIGURATION);
		List<ItemCategories> itemsList = (List<ItemCategories>) CommonFunctionDelegator.getObjectsByCriterion(criterionList, null, null, ItemCategories.class);

		this.content = RSSFeedGenerator.getContent(itemsList, language, configuration.getSiteName(), request.getRequestURL().toString(), catId);

		return SUCCESS;
	}


	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getContent() {
		return content;
	}
}
